﻿var FCKPageCombo_command = function(name) {
    this.Name = name ;
}
//This get's executed when an item from the combo list gets selected
FCKPageCombo_command.prototype.Execute =function(itemText, itemLabel) {
    var dialog	= window.parent ;
    //var oEditor = dialog.InnerDialogLoaded() ;
    var sUri, sInnerHtml ;
    
    var oLink = FCKSelection.MoveToAncestorNode( 'A' ) ;
    if ( oLink )
        FCK.Selection.SelectNode( oLink ) ;

	//oEditor.FCKUndo.SaveUndoStep() ;

    var matches = /(url=")(.*)(")/.exec(itemText);
    var txt = matches[2];
    sUri = txt;

	// If no link is selected, create a new one (it may result in more than one link creation - #220).
	var aLinks = oLink ? [ oLink ] : FCK.CreateLink( sUri, true ) ;

	// If no selection, no links are created, so use the uri as the link text (by dom, 2006-05-26)
	var aHasSelection = ( aLinks.length > 0 ) ;
	if ( !aHasSelection )
	{
		sInnerHtml = itemLabel.innerHTML;
		// Create a new (empty) anchor.
		aLinks = [ FCK.InsertElement( 'a' ) ] ;
	}

	for ( var i = 0 ; i < aLinks.length ; i++ )
	{
		oLink = aLinks[i] ;

		if ( aHasSelection )
			sInnerHtml = oLink.innerHTML ;		// Save the innerHTML (IE changes it if it is like an URL).

		oLink.href = sUri ;
		SetAttribute( oLink, '_fcksavedurl', sUri ) ;

		oLink.innerHTML = sInnerHtml ;		// Set (or restore) the innerHTML
	}

	// Select the (first) link.
	FCKSelection.SelectNode( aLinks[0] );

	return true ;
}
//was getting GetState is not a function (or similar) errors, so added this.
FCKPageCombo_command.prototype.GetState = function() {
    return;
}

FCKCommands.RegisterCommand( 'pagecombocommand' , new FCKPageCombo_command('pagecombocommand') ) ;

var FCKToolbarPageCombo=function(tooltip,style){
    this.Command=FCKCommands.GetCommand('pagecombocommand');//the command to execute when an item is selected
    this.CommandName = 'pagecombocommand';
    this.Label=this.GetLabel();
    this.Tooltip=tooltip?tooltip:this.Label; //Doesn't seem to work
    this.Style=style; //FCK_TOOLBARITEM_ICONTEXT OR FCK_TOOLBARITEM_ONLYTEXT
};
FCKToolbarPageCombo.prototype=new FCKToolbarSpecialCombo;
//Label to appear in the FCK toolbar
FCKToolbarPageCombo.prototype.GetLabel=function(){
    return "<b>Pages</b>";
};
//Add the items to the combo list
FCKToolbarPageCombo.prototype.CreateItems=function(A){
    try {
    	if (parent.arrPagesItems != undefined) {
    		if (parent.arrPagesItems.length == 0) {
    			this._Combo.AddItem('', '<span style="font-size: 9pt;">No items available</span>');
    		} else {
    			for (var i = 0; i < parent.arrPagesItems.length; i++)
    				this._Combo.AddItem(parent.arrPagesItems[i][0], parent.arrPagesItems[i][1]);
    		}
    	} else {
    		this._Combo.AddItem('', '<span style="font-size: 9pt;">No items available</span>');
    	}
    } catch(err) {
    	this._Combo.AddItem('', '<span style="font-size: 9pt;">No items available</span>');
    }
}

//Register the combo with the FCKEditor
FCKToolbarItems.RegisterItem( 'PageCombo' , new FCKToolbarPageCombo( 'Page Combo', FCK_TOOLBARITEM_ICONTEXT ) ) ; //or FCK_TOOLBARITEM_ONLYTEXT

//********END********


function SetAttribute( element, attName, attValue )
{
	if ( attValue == null || attValue.length == 0 )
		element.removeAttribute( attName, 0 ) ;			// 0 : Case Insensitive
	else
		element.setAttribute( attName, attValue, 0 ) ;	// 0 : Case Insensitive
}
